#!/usr/bin/python
# -*- coding: utf-8 -*-  
import re  
import urllib  
import urllib2  
import cookielib  
from bs4 import BeautifulSoup
from mypost import *

#模拟登录  
cj = cookielib.CookieJar()  
#用户名和密码
#form_data =  {'name': 'qing', 'pass': 'qing@drupal', 'op':'登录'}  
form_data =  {'name': 'qing', 'pass': 'qing2drupal', 'form_id':'user_login_block'}  
post_data = urllib.urlencode(form_data)
#登录路径  
path = 'http://127.0.0.1:8080/drupal/?q=node&destination=node'
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  
opener.addheaders = [('User-agent', 'Opera 9.2'), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
('Accept-Language','zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3'),
('Accept-Encoding','gzip, deflate'),
('Connection','keep-alive')]
urllib2.install_opener(opener) 
req = urllib2.Request(path, post_data)  
conn = urllib2.urlopen(req)

article_data = {'title':'Title0', 
'field_tags[und]':'tags', 
'body[und][0][value]':'body0',
'body[und][0][format]':'filtered_html',
'files[field_image_und_0]':'1',
'field_image[und][0][fid]':'1',
'field_image[und][0][display]':'1',
'changed':'1',
'form_build_id':'1',
'form_token':'1',
'form_id':'article_node_form',
'menu[link_title]':'1',
'menu[description]':'1',
'menu[parent]':'main-menu',
'menu[weight]':'0',
'log':'1',
'path[pathauto':'1',
'comment':'2',
'name':'qing',
'date':'1',
'status':'1',
'promote':'1',
'additional_settings_active_tab':'edit-menu',
'op':'保存'
}

add_path = 'http://127.0.0.1:8080/drupal/node/add/article'
files = {'filename':'1'}
posturl(add_path, article_data, files)
#add_req = urllib2.Request(add_path, article_data)
#add_conn = urllib2.urlopen(add_req)
#print add_conn.read()

